package com.etocrm.saas.lib.base.common.obj;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;

/**
 * 分页查询参数基类
 */
@Getter
@Setter
public class BasePageDto extends BaseDto {

    public static final long PAGE_SIZE_DEFAULT = 20L;
    public static final long PAGE_SIZE_MAX = 500L;
    public static final long PAGE_MAX = (long) (Integer.MAX_VALUE - 1);


    protected Long currentPage;
    protected Long pageSize;

    /**
     * 当前页码，从1开始计数
     */
    public Long getCurrentPage() {
        if (currentPage == null || currentPage <= 0) {
            return 1L;
        }
        if (currentPage > PAGE_MAX) {
            return PAGE_MAX;
        }
        return currentPage;
    }

    /**
     * 每页大小
     */
    public Long getPageSize() {
        if (pageSize == null || pageSize < 0) {
            return PAGE_SIZE_DEFAULT;
        }
        if (pageSize > PAGE_SIZE_MAX) {
            return PAGE_SIZE_MAX;
        }
        return pageSize;
    }

    /**
     * 当前分页起始下标
     */
    @JsonIgnore
    public int getPageFromIndex() {
        long l = (getCurrentPage() - 1) * getPageSize();
        return (int) l;
    }

    /**
     * 当前分页终止下标
     */
    @JsonIgnore
    public int getPageToIndex() {
        long l = getPageFromIndex() + getPageSize() - 1;
        return (int) l;
    }

}
